@extends('admin.common.base-master')

@section('html-head')
    {{--<link href="{{url('/vendor/tyupload/control/css/zyUpload.css')}}" type="text/css" rel="stylesheet" />--}}
    <link href="{{url('css/jquery.form.css')}}" type="text/css" rel="stylesheet" />
    {{--<script src="{{url('/vendor/My97DatePicker/WdatePicker.js')}}"></script>--}}
    <script src="{{url('/js/lib/jquery.form.js')}}"></script>


    <link rel="stylesheet" href="{{url('css/admin/jquery-ui.css')}}">
    <script type="text/javascript" src="{{url('css/admin/jquery-ui.min.js')}}"></script>
    <link href="{{url('js/lib/jquery-ui-timepicker-addon.css')}}" type="text/css" />
    <script src="{{url('js/lib/jquery-ui-timepicker-addon.js')}}" type="text/javascript"></script>
    <script src="{{url('js/lib/jquery.ui.datepicker-zh-CN.js')}}" type="text/javascript"></script>
    <script src="{{url('js/lib/jquery-ui-timepicker-zh-CN.js')}}" type="text/javascript"></script>

    <link rel="stylesheet" href="{{url('css/admin/bootstrap.min.css')}}">
    <link rel="stylesheet" href="{{url('css/admin/bootstrap-multiselect.css')}}">
    <script src="{{url('js/lib/bootstrap.min.js')}}" type="text/javascript"></script>
    <script src="{{url('js/lib/bootstrap-multiselect.js')}}" type="text/javascript"></script>


    <style>
        .btn_add_classify{
            background: #31bcd3;margin-left:10px;height: 5px;width: 50px;color: white;text-align: center;padding:5px;border-radius:7px;box-shadow: 3px 2px 9px #888888;
        }
        .discount_multiselect .btn-group .btn{
            background-color: #31bcd3;
        }
        .discount_multiselect .btn-group .btn:hover{
            color: #FFFFFF;
        }
        * {
            box-sizing: content-box;
        }
        a:hover{
            text-decoration: none;
        }
    </style>
    @include('UEditor::head')
@endsection

@section('html-body')
    <!-- content start -->

    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="#">商品管理</a>
            <em>></em>
            <a href="{{route('Admin.Home.goodsList')}}">商品列表</a>
            <em>></em>
            <a href="={{route('Admin.Home.goodsSet')}}">商品设置</a>
        </div>

        @include('admin.common.goods-header')

        <div class="gg-step">
            <form  id="advform">
                <input type="hidden" name="id" id="goodsid" value="@if($sysGoods && $sysGoods->id){{$sysGoods->id}} @endif"/>
                <div class="gg-list">
                    <label class="tit">商品编号</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入商品货号" name="ssn" id="ssn" value="@if($sysGoods && $sysGoods->ssn){{$sysGoods->ssn}} @endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">可输入1-20个字符（一个汉字等于2个字符）</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">商品名称</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入商品名称" name="name" id="name" value="@if($sysGoods && $sysGoods->name){{$sysGoods->name}} @endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">输入1-50个字符(一个汉字等于两个字符)</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">市场价格</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入市场价格" name="m_price" id="m_price" value="@if($sysGoods && $sysGoods->m_price){{$sysGoods->m_price}} @endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">输入数值(小数或整数)</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">销售价格</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入销售价格" name="s_price" id="s_price" value="@if($sysGoods && $sysGoods->s_price){{$sysGoods->s_price}} @endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">输入数值(小数或整数)</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">是否推荐</label>
                    <div class="cont">
                        <select name="is_recommend" id="is_recommend">
                            <option value="0" @if($sysGoods && $sysGoods->is_recommend == 0) selected @endif>不推荐</option>
                            <option value="1" @if($sysGoods && $sysGoods->is_recommend == 1) selected @endif>推荐</option>
                        </select>
                        <p class="text"></p>
                    </div>
                </div>

                <div class="gg-list" data-id="ggzst2">
                    <label class="tit">商品封面图</label>
                    <div class="demo" style="display: inline-block; padding: 0; margin: 0; *display: inline; *zoom:1;">
                        <input type = "hidden" name = "pic_img" id = "pic_img2" value = "" height="70px" >
                        <div id="showimg">
                        </div>
                        <div class="btn" style="background: #00A2D4">
                            <span id="inputImage">添加图片</span>
                            <input id="img_id" type="hidden" name="thumb_id" value="@if($sysGoods && $sysGoods->thumb_id){{$sysGoods->thumb_id}} @endif">
                        </div>
                        <div class="progress">
                            <span class="bar"></span><span class="percent">0%</span >
                        </div>
                        <p class="text" id="uploadthumb">上传详情图（图片格式为jpg、png、gif，规格：高限315px,文件小于2MB）</p>
                        @if($sysGoods && $sysGoods->thumb_id != 0)<img id="img_show" style="width: 100px;padding-top: 3px;" src="{{\App\Http\Services\FileManager::getImgUrl($sysGoods->thumb_id)}}" />@endif

                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">是否包邮</label>
                    <div class="cont">
                        <select name="is_free_shipping" id="is_free_shipping">
                            <option value="0" @if($sysGoods && $sysGoods->is_free_shipping == 0) selected @endif>不包邮</option>
                            <option value="1" @if($sysGoods && $sysGoods->is_free_shipping == 1) selected @endif>包邮</option>
                        </select>
                        <p class="text"></p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">商品重量(kg)</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入商品重量" name="weight" id="weight" value="@if($sysGoods && $sysGoods->weight){{$sysGoods->weight}}@endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">输入数值(小数或整数)</p>
                    </div>
                </div>

                <div class="gg-list" data-id="ggwz">
                    <label class="tit">商品信息：</label>
                    <div class="cont">
                        <textarea placeholder="请输入商品信息" name="info" id="info" cols="70" rows="10">@if($sysGoods && $sysGoods->info){{$sysGoods->info}} @endif</textarea>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">详情</label>
                    <div class="cont">
                        <textarea name="description" id="aa"></textarea>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">所属分类</label>
                    <div class="cont">
                        <label class="gg-radio">
                            <select id="classify">
                                @if(!$sysGoods || $sysGoods->classify_id == null || $sysGoods->classify_id != '')
                                    <option value="0">请选择分类</option>
                                @endif
                                @foreach($parent_classifys as $classify)
                                    <option value="{{$classify->id}}" @if($sysGoods && $sysGoods->classify_id == $classify->id)selected @endif>{{$classify->name}}</option>
                                @endforeach
                            </select>
                        </label>
                        <label class="gg-radio">
                            <select id="classify_two">
                                @if(!is_null($child_classifys) && count($child_classifys) > 0)
                                    <option value="0">请选择二级分类</option>
                                @foreach($child_classifys as $classify)
                                        <option value="{{$classify->id}}" @if($sysGoods && $sysGoods->classify_two_id == $classify->id)selected @endif>{{$classify->name}}</option>
                                    @endforeach
                                @else
                                    <option value="0">无二级分类</option>
                                @endif
                            </select>
                        </label>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">商品状态</label>
                    <div class="cont">
                        <select name="is_shop" id="is_shop">
                            <option value="1" @if($sysGoods && $sysGoods->is_shop == 1) selected @endif>上架</option>
                            <option value="0" @if($sysGoods && $sysGoods->is_shop == 0) selected @endif>下架</option>
                        </select>
                        <p class="text">选择上架或下架 ( 默认上架 )</p>
                    </div>
                </div>

                <div class="gg-list">
                    <label class="tit">促销选择</label>

                    <input id="no_discount" name="discount" type="radio" style="margin-top: 10px">无优惠
                    <input id="hand_discount"  name="discount"checked="checked" type="radio" style="margin-top: 10px;margin-left: 20px;">优惠
                    <div class="cont discount_multiselect">
                        <select id="discount_multiselect" class="multiselect" multiple="multiple">
                            @if(count($rules) > 0)
                                @foreach($rules as $rule)
                                    <option value="{{$rule->id}}" @if(in_array($rule->id,$exist_rule_ids)) selected @endif>{{$rule->name}}</option>
                                @endforeach
                            @endif
{{--                            <option value="中国">中国</option>--}}
{{--                            <option value="美国">美国</option>--}}
{{--                            <option value="大胖">大胖</option>--}}
{{--                            <option value="中胖">中胖</option>--}}
{{--                            <option value="小胖">小胖</option>--}}
                        </select>
                    </div>
                </div>

{{--                <div class="gg-list">--}}
{{--                    <label class="tit">上架时间</label>--}}
{{--                    <div class="cont">--}}
{{--                        <input class="gg-ipt" type="text" placeholder="请输入上架时间" name="shop_time" id="shop_time" value="@if($sysGoods && $sysGoods->shop_time){{$sysGoods->shop_time}} @endif" /><cite id="title_re" class="required">*</cite>--}}
{{--                        <p class="text">输入数值(小数或整数)</p>--}}
{{--                    </div>--}}
{{--                </div>--}}


{{--                <div class="gg-list">--}}
{{--                    <label class="tit">积分</label>--}}
{{--                    <div class="cont">--}}
{{--                        <input class="gg-ipt" type="text" placeholder="请输入购买商品所获的积分数量" name="integral" id="integral" value="@if($sysGoods){{$sysGoods->integral}} @endif" /><cite id="title_re" class="required">*</cite>--}}
{{--                        <p class="text">输入数值(注意是整数)</p>--}}
{{--                    </div>--}}
{{--                </div>--}}

                <div class="gg-list">
                    <label class="tit">商品排序</label>
                    <div class="cont">
                        <input class="gg-ipt" type="text" placeholder="请输入商品排序" name="sort" id="sort" value="@if($sysGoods){{$sysGoods->sort}} @endif" /><cite id="title_re" class="required">*</cite>
                        <p class="text">输入1-100位的数值，值越小越靠前(注意是整数)</p>
                    </div>
                </div>

            </form>
            <div class="gg-list" >
                <label class="tit">&nbsp;</label>
                <div class="cont" >
                    <a href="javascript:;" class="next-step" id="btn-set">提交</a>
                </div>
            </div>
        </div>
    </div>





    <!-- content end -->
    <script>
        $(function() {

            //促销下拉多选框初始化
            var discountList = [] //选择的促销列表
            var optionSelectList = []
            if($('#no_discount').is(':checked')) {
                $('.discount_multiselect').attr("style","display:none;")
            }
            $('#discount_multiselect').multiselect({
                buttonClass: 'btn',
                buttonWidth: 'auto',
                buttonText: function(options) {
                    if (options.length == 0) {
                        optionSelectList = []
                        discountList = []
                        discountList.push(0)
                        console.log(discountList)
                        return '请选择';
                    }
                    else {
                        var selected = '';
                        discountList = []
                        optionSelectList = []

                        options.each(function() {
                            optionSelectList.push($(this).val())
                            selected += $(this).text() + ',';
                        });
                        if($('#no_discount').is(':checked')) {
                            discountList = [0]
                        } else{
                            discountList = optionSelectList
                        }

                        console.log(discountList)
                        return selected.substr(0, selected.length -1) ;
                    }

                },
            });

            //单选框点击事件
            $('#no_discount').click(function (e) {
                discountList = []
                discountList.push(0)
                console.log(discountList)
                $('.discount_multiselect').attr("style","display:none;")
            })
            $('#hand_discount').click(function (e) {
                if(optionSelectList.length == 0){
                    discountList = []
                    discountList.push(0)
                } else{
                    discountList = optionSelectList
                }
                console.log(discountList)
                $('.discount_multiselect').removeAttr("style","display:none;")
            })

            jQuery('#shop_time').datetimepicker({
                timeFormat: "HH:mm:ss",
                dateFormat: "yy-mm-dd",
                beforeShow: function () {
                    setTimeout(
                        function () {
                            $('#ui-datepicker-div').css("z-index", 1000);
                        }, 100
                    );
                }
            });

            $('#classify').change(function(){
                var id = $('#classify option:selected').val();
                if(parseInt(id) != 0){
                    $.ajax({
                        url:"{{route('Api.Admin.sys.getChildClassify')}}",
                        type:"post",
                        dataType:"json",
                        data:{id:id},
                        success:function (obj) {
                            if(obj.level == 'success'){
                                appendClassify(obj.data);
                            }else {
                                noty({'type':'error','text':obj.message,timeout:600});
                            }
                        },
                        error:function (obj) {
                            console.log(obj);
                        }
                    });
                }
            });

            //写入部门
            function appendClassify(data) {
                $('#classify_two').empty();
                $('#classify_two').append('<option value="0" selected>请选择二级分类</option>');
                if(data.length > 0){
                    for(var i = 0;i < data.length;i++){
                        var htmlElent = '<option value="'+data[i].id+'">'+data[i].name+'</option>';
                        $('#classify_two').append(htmlElent);
                    }
                }
            }

            $(document).on('click', '#btn-set', function () {
                var info         = $('#info').val();
                var ssn          = $('#ssn').val();
                var name         = $('#name').val();
                var mPrice       = $('#m_price').val();
                var sPrice       = $('#s_price').val();
                var imgId        = $('#img_id').val();
                var isShop       = $('#is_shop option:selected').val();
                var isRecommend  = $('#is_recommend option:selected').val();
                var isSelling    = $('#is_selling option:selected').val();
                var isNew        = $('#is_new option:selected').val();
                var isFreeShipping=$('#is_free_shipping option:selected').val();
                var sort         = $('#sort').val();
                var classifyId   = $('#classify option:selected').val();
                var classifyTwo = $('#classify_two option:selected').val();
                // var shopTime     = $('#shop_time').val();
                var weight     = $('#weight').val();
                var integral     = $("#integral").val();
                var id           = $('#goodsid').val();
                var detail       = '';
                ue.ready(function() {
                    detail = ue.getContent();
                });

                $.ajax({
                    url: '{{route("Api.Admin.goods.setGoods")}}',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        ssn:ssn,
                        name:name,
                        m_price:mPrice,
                        s_price:sPrice,
                        thumb_id:imgId,
                        info:info,
                        is_shop:isShop,
                        is_recommend:isRecommend,
                        is_selling:isSelling,
                        is_new:isNew,
                        isFreeShipping:isFreeShipping,
                        sort: sort,
                        // shop_time:shopTime,
                        // integral:integral,
                        classify_id:classifyId,
                        classify_two_id:classifyTwo,
                        id:id,
                        type:"common",
                        weight:weight,
                        detail:detail,
                        discount_sales_rule_ids: discountList
                    },
                    success: function (obj) {
                        if (obj.level == "success") {   
                            noty({
                                text: obj.message, type: obj.level, timeout: 600,
                                callback: {
                                    afterClose:function(){
                                        window.history.go(-1);
                                    }
                                }
                            });
                        } else if (obj.level == "error") {
                            noty({text: obj.message, type: obj.level, timeout: 600});
                        }

                    },
                    error: function (obj) {
                        console.log(obj);
                    }
                });
            });
            <!-- 实例化编辑器 -->
            var ue = UE.getEditor('aa',{
                autoHeightEnabled: true,
                initialFrameHeight:400,
                initialFrameWidth:600,
                toolbars: [
                    ['fullscreen', 'source','undo','redo','|','fontsize','horizontal','|','simpleupload','insertvideo','removeformat'],
                    ['bold', 'italic', 'underline', 'forecolor', 'backcolor','|','indent','|','justifyleft', 'justifyright', 'justifycenter', 'justifyjustify', '|', 'rowspacingtop', 'rowspacingbottom', 'lineheight','|','insertunorderedlist', 'insertorderedlist','|', 'imagenone','imageleft','imageright','imagecenter','|','link']
                ]
            });

            @if($sysGoods)
            ue.ready(function() {
                ue.setContent('<?php echo $sysGoods->detail ?>');
            });
            @endif

            //缩略图上传代码
            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                browse_button: 'inputImage',
                url: '{{route("Api.File.upload")}}',

                chunk_size: '20mb',
                filters: {
                    max_file_size: '20mb',
                    mime_types: [
                        {title: "Image files", extensions: "jpg,gif,png"}
                    ],
                    prevent_duplicates: false
                },
                multi_selection: true,
                unique_names: true,

                flash_swf_url: '{{ url('vendor/plupload-2.1.2/Moxie.swf') }}',
                silverlight_xap_url: '{{ url('vendor/plupload-2.1.2/Moxie.xap') }}',

                file_data_name: 'photo',
                headers: {
                    Accept: 'application/json'
                },

                multipart_params: {},

                init: {
                    PostInit: function () {
                    },

                    FilesAdded: function (up, files) {
                        up.start();
                    },

                    BeforeUpload: function (up, file) {
                        up.settings.multipart_params.file_name = file.name;
                    },

                    UploadProgress: function (up, file) {
                        $('#uploadthumb').html('<span style="color:red">上传中...</span>');
                    },

                    Error: function (up, err) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(err.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }

                        noty({
                            type: 'error',
                            text: err.message + ' ' + responseMessage,
                            timeout: 800
                        });
                    },

                    FileUploaded: function (up, file, info) {
                        var responseObj = '', responseMessage = '';
                        try {
                            responseObj = $.parseJSON(info.response);
                            responseMessage = responseObj.message;
                        } catch ($e) {

                        }
                        if (responseObj.level == "success") {
                            $('#img_id').val(responseObj.data.id);
                            var imgURL = responseObj.data.img_url + "?" + Math.random();
                            $('#img_show').attr("src",imgURL);
                            $('#uploadthumb').html('<span style="color:green">上传成功</span>');
                        } else {
                            noty({type: responseObj.level, text: responseObj.message, timeout: 800});
                            $('#uploadthumb').html('<span style="color: red">上传失败，请重新上传！</span>');
                        }

                    }
                }
            });
            uploader.init();
        });
    </script>

@endsection